package org.phytodata.actionengine.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.platform.api.repository.ISolutionRepository;
import org.pentaho.platform.engine.core.system.PentahoSystem;
import org.pentaho.platform.engine.core.system.StandaloneSession;
import org.pentaho.platform.engine.services.messages.Messages;

public class LegacyEngineUtil {
  
  private static final Log log = LogFactory.getLog(LegacyEngineUtil.class);
  private static final String PLUGIN_BUNDLE_NAME = "org.pentaho.platform.engine.services.runtime.plugins";
  
  @SuppressWarnings("unchecked")
  /**
   * Adapted from org.pentaho.platform.engine.services.runtime.RuntimContext
   * @return map of component alias and classname
   */
  public static Map<String, String> getLegacyComponents() {
    Properties knownComponents = new Properties();
    // First, get known plugin names...
    try {
      ResourceBundle pluginBundle = ResourceBundle.getBundle(PLUGIN_BUNDLE_NAME);
      if (pluginBundle != null) { // Copy the bundle here...
        Enumeration keyEnum = pluginBundle.getKeys();
        String bundleKey = null;
        while (keyEnum.hasMoreElements()) {
          bundleKey = (String) keyEnum.nextElement();
          knownComponents.put(bundleKey, pluginBundle.getString(bundleKey));
        }
      }
    } catch (Exception ex) {
      log.warn(Messages.getInstance().getString("RuntimeContext.WARN_NO_PLUGIN_PROPERTIES_BUNDLE")); //$NON-NLS-1$
    }
    // Get overrides...
    //
    // Note - If the override wants to remove an existing "known" plugin, 
    // simply adding an empty value will cause the "known" plugin to be removed.
    //
    if( !PentahoSystem.getObjectFactory().objectDefined( ISolutionRepository.class.getSimpleName() ) ) {
      // this is ok
      return new HashMap<String, String>((Map)knownComponents);
    }
    ISolutionRepository solutionRepository = PentahoSystem.get(ISolutionRepository.class, new StandaloneSession("system")); //$NON-NLS-1$
    try {
      if( solutionRepository.resourceExists( "system/plugin.properties", ISolutionRepository.ACTION_EXECUTE ) ) { //$NON-NLS-1$
        InputStream is = solutionRepository.getResourceInputStream("system/plugin.properties", false, ISolutionRepository.ACTION_EXECUTE); //$NON-NLS-1$
        Properties overrideComponents = new Properties();
        overrideComponents.load(is);
        knownComponents.putAll(overrideComponents); // load over the top of the known properties
      }
    } catch (FileNotFoundException ignored) {
      log.warn(Messages.getInstance().getString("RuntimeContext.WARN_NO_PLUGIN_PROPERTIES")); //$NON-NLS-1$
    } catch (IOException ignored) {
      log.warn(Messages.getInstance().getString("RuntimeContext.WARN_BAD_PLUGIN_PROPERTIES"), ignored); //$NON-NLS-1$
    }

    return new HashMap<String, String>((Map)knownComponents);
  }

}